/**
 * Created by qing on 2017/4/11.
 * 排序
 */
$(document).ready(function () {
    Array.prototype.insert = function (index, item) {
        this.splice(index, 0, item);
    };
    //VUE数据绑定对象
    var addValuesVue = null;
    var pid = app.getUrlParam("PID");
    //定义请求连接
    var callUrl = '';

    ////排序界面
    //var callMaxSeq = app.callAjaxPostSync("../../../../api/BaseDictionaryController/selectChildBypId", {PID: pid});
    //
    //var result = app.callAjaxPostSync("../../../../api/GoodsMainController/selectById", {ID: para_Id});

    addValuesVue = new Vue({
            el: '#dictionarySorting',
            data: {
                myParams: {
                    PARACODE: '',//编号
                    PARANAME: ''//名称
                    //SEQUENCE: ''//callMaxSeq.success ? callMaxSeq.data : ''//次序
                }
            }
        });
    var queryTime=0;
    var curRow={};
    var sortGrid = $("#table").bootstrapTable({
        columns:[{
                halign: 'center',
                title: '序号',
                sortable: true,
                formatter:function(value,row,index){
                    row.index=index;
                    return index+1;
                }
            }, {
                field: 'PARACODE',
                halign: 'center',
                title: '编码',
                sortable: true,
                cellStyle: function (value, row, index) {
                    return {
                        css: {
                            "overflow": "hidden",
                            "text-overflow": "ellipsis",
                            "white-space": "nowrap"
                        }
                    };
                }
            }, {
                field: 'PARANAME',
                halign: 'center',
                title: '名称',
                sortable: true,
                cellStyle: function (value, row, index) {
                    return {
                        css: {
                            "overflow": "hidden",
                            "text-overflow": "ellipsis",
                            "white-space": "nowrap"
                        }
                    };
                }
            }],
        sortable: false,//是否可以排序，每一列都可以进行指定
        url: "../../../../api/BaseDictionaryController/selectChildBypId",//加载时调用的后台连接
        undefinedText: '-',//字段值为undefine时显示的内容
        // showRefresh: true,//是否显示刷新按钮
        //showColumns: true,//是否显示列头
        striped: true,//是否显示表格线
        //showExport: true, //是否显示导出按钮
        //导出时的文件信息配置
        // exportOptions: {
        //     fileName: '用户信息',
        //     worksheetName: '用户信息'
        // },
        // exportDataType: "all", //导出的数据类型
        // exportTypes: ['excel'],//导出文件类型
        iconSize: "outline",//icon图标的显示方式
        toolbar: "#busGrdToolbar",//绑定表格的操作栏，就是新增、修改、删除按钮所在div的ID
        icons: {
            refresh: "glyphicon-repeat",
            toggle: "glyphicon-list-alt",
            columns: "glyphicon-list"
        },
        pagination: false,//是否分页
        sidePagination: "server", // 表示分页时进行服务端请求
        method: 'post',//请求方法[post/get]
        dataField: 'data',
        pageNumber: 1,//页面分页的初始页码
        pageSize: 5,//每页显示数量(暂定每页10条，请暂勿修改，保持统一)
        pageList: [5, 10, 25, 50, 100],//每页显示的可选数量
        queryParamsType: "undefined",

        //查询参数配置
        queryParams: function (params) {
            var pIndex, myTotal;
            if (queryTime === 0) {
                pIndex = 1;
                myTotal = -1;
                queryTime++;
            }
            else {
                pIndex = params.pageNumber;
                myTotal = this.totalRows;
            }
            return {
                limit: params.pageSize,
                page: pIndex,
                allcount: myTotal,
                data: {id:pid},
                sortName: params.sortName,
                sortOrder: params.sortOrder
            }
        },
        //排序事件
        onSort: function (name, order) {
            sortGrid.queryData();
        },
        onClickRow: function (row, $element) {
            curRow = row;
            $element.parent("tbody").children("tr").css({
                "background-color": "#f9f9f9",
                "color": "black"
            });
            $element.css({
                "background-color": "#8CD3EC",
                "color": "white"
            });
        },
        //数据加载完毕事件
        onLoadSuccess: function (result) {

        }
    });
    sortGrid.queryData = function () {
        queryTime = 0;
        this.bootstrapTable('refresh', {
            silent: false
        });
    };

    //判断只能选取一条数据
    if ($("input[type='checkbox']:checked").length>1){
        layer.alert("只能选取一条数据",{icon: 0})
    }

    $('#btnDetermine').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        if(sortList){
            for(var i=0;i<sortList.length;i++){
                var dicItem=sortList[i];
                dicItem.SEQUENCE=i;
            }
        }
        var loadIndex=loading();
        loadIndex.show();
        app.callAjaxPost('../../../../api/BaseDictionaryController/updateList',sortList,function(result){
            loadIndex.close();
            if (result.success) {
                //提示是否成功，并且回调关闭当前弹窗
                layer.alert(result.message, {icon: 1}, function (index) {
                    parent.layer.close(parent.layer.getFrameIndex(window.name)); //再执行关闭
                });
            } else {
                layer.alert(result.message, {icon: 2});
            }
        });
    });


    $('#btnUP').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        var index=curRow.index;
        if(sortList){
            if(index&&index>0){
                sortList.splice(index, 1);
                sortList.unshift();
                sortList.insert(index-1,curRow);
                curRow=sortList[index-1];
                sortGrid.bootstrapTable('load',sortList);
                var el = '#table > tbody > tr:eq(' + (index - 1).toString() + ')';
                $(el).css({
                    "background-color": "#8CD3EC",
                    "color": "white"
                });
            }
        }

    });

    $('#btnTop').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        var index=curRow.index;
        if(sortList){
            if(index){
                sortList.splice(index, 1);
                sortList.unshift(curRow);
                curRow=sortList[0];
                sortGrid.bootstrapTable('load',sortList);
                $('#table > tbody > tr:eq(0) ').css({
                    "background-color": "#8CD3EC",
                    "color": "white"
                });
            }
        }

    });
    $('#btnDown').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        var index=curRow.index;
        if(sortList){
            if(index<sortList.length-1){
                sortList.splice(index, 1);
                sortList.insert(index+1,curRow);
                curRow=sortList[index+1];
                sortGrid.bootstrapTable('load',sortList);
                var el = '#table > tbody > tr:eq(' + (index + 1).toString() + ')';
                $(el).css({
                    "background-color": "#8CD3EC",
                    "color": "white"
                });
            }
        }
    });
    $('#btnBottom').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        var length = sortList.length;
        var index = curRow.index;
        if (sortList) {
            if (index < length - 1) {
                sortList.splice(index, 1);
                sortList.push(curRow);
                curRow = sortList[sortList.length - 1];
                sortGrid.bootstrapTable('load', sortList);
                $('#table > tbody > tr:eq(-1) ').css({
                    "background-color": "#8CD3EC",
                    "color": "white"
                });

            }
        }
    });


    //$(function(){
    //    //上移
    //    var $up = $(".btnUP");
    //    $up.click(function() {
    //        var $tr = $(this).parents("tr");
    //        if ($tr.index() != 0) {
    //            $tr.fadeOut().fadeIn();
    //            $tr.prev().before($tr);
    //
    //        }
    //    });
    //    //下移
    //    var $down = $(".btnDown");
    //    var len = $down.length;
    //    $down.click(function() {
    //        var $tr = $(this).parents("tr");
    //        if ($tr.index() != len - 1) {
    //            $tr.fadeOut().fadeIn();
    //            $tr.next().after($tr);
    //        }
    //    });
    //    //置顶
    //    var $top = $(".btnTop");
    //    $top.click(function(){
    //        var $tr = $(this).parents("tr");
    //        $tr.fadeOut().fadeIn();
    //        $(".table").prepend($tr);
    //        $tr.css("color","#f60");
    //    });
    //});
    //




    ////排序界面的暂存按钮
    //$('#btnDetermine').bind('click', function () {
    //    var formValues = {};
    //    formValues = addValuesVue.myParams;
    //    app.callAjaxPost(callUrl, formValues, function (result) {
    //        if (result.success) {
    //            //提示是否成功，并且回调关闭当前弹窗
    //            parent.layer.close(parent.layer.getFrameIndex(window.name)); //再执行关闭
    //            parent.layer.alert(result.message, {icon: 1});
    //        } else {
    //            layer.alert(result.message, {icon: 2});
    //        }
    //    }, this);
    //});

    //排序界面的 返回 按钮
    $('#btnCancel').bind('click', function () {
        //当你在iframe页面关闭自身时
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });




    //深度克隆
    function deepClone(obj) {
        var result, oClass = isClass(obj);
        //确定result的类型
        if (oClass === "Object") {
            result = {};
        } else if (oClass === "Array") {
            result = [];
        } else {
            return obj;
        }
        for (key in obj) {
            var copy = obj[key];
            if (isClass(copy) == "Object") {
                result[key] = arguments.callee(copy);//递归调用
            } else if (isClass(copy) == "Array") {
                result[key] = arguments.callee(copy);
            } else {
                result[key] = obj[key];
            }
        }
        return result;
    }

//返回传递给他的任意对象的类
    function isClass(o) {
        if (o === null) return "Null";
        if (o === undefined) return "Undefined";
        return Object.prototype.toString.call(o).slice(8, -1);
    }
});

